﻿#define _CRT_SECURE_NO_WARNINGS
/*
#include <stdio.h>
//#include <stdio.h>
#include <stdlib.h>
// hu是命名空间的名字，⼀般开发中是⽤项⽬名字做命名空间名
namespace hu {
	// 命名空间中可以定义变量/函数/类型
	int rand = 10;
	int  add(int x, int y) {
		return x + y;
	}
	//定义一个节点
	struct Node
	{
		struct Node* next;
		int val;
	};
}
int main()
{
	hu::Node* node = (hu::Node*)malloc(sizeof(hu::Node));
	node->val = 1;
	// 这⾥默认是访问的是全局的rand函数指针
	printf("%p\n", rand);
	// 这⾥指定hu命名空间中的rand
	printf("%d\n", hu::rand);
	printf("%d\n", hu::add(2, 3));
	printf("%d\n", node->val);
	return 0;
}
*/
/*
#include<stdio.h>
namespace bit
{
	int a = 0;
	int b = 1;
}
int main()
{
	// 编译报错：error C2065: “a”: 未声明的标识符
	printf("%d\n", a);
	return 0;
}

#include <stdio.h>
namespace N {
	int a = 520;
	int b = 1314;
}
int main()
{  
	// 指定命名空间访问
	printf("%d\n", N::a);
	// using将命名空间中某个成员展开
	using N::b;
	printf("%d\n", b);
	return 0;
}
*/
/*
#include <iostream>
using namespace std;
int main() {
	int a = 10;
	double b = 5.2;
	char c = 'd';
	cout << a << " " << b << " " << c << endl;
	std::cout << a << " " << b << " " << c << std::endl;
	scanf("%d %lf", &a, &b);
	printf("%d %lf", a, b);

	cin >> a;
	cin >> b >> c;
	cout << a << endl;
	cout << b << " " << c << endl;
	return 0;
}

#include <iostream>
using namespace std;
void func(int x=0) {
	cout << x << endl;
}
int main() {
// 没有传参时，使⽤参数的默认值
// 传参时，使⽤指定的实参
	func();
	func(10);
	return 0;
}
*/
/*
#include <iostream>
using namespace std;
// 全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
// 半缺省
void Func2(int a, int b = 10, int c = 20)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
int main()
{
	Func1();
	Func1(1);
	Func1(1, 2);
	Func1(1, 2, 3);
	Func2(100);
	Func2(100, 200);
	Func2(100, 200, 300);
	return 0;
}
*/
//#include <iostream>
//using namespace std;
/*
int add(int a, int b) {
	cout << "int add(int a, int b)"<<endl;
		return a + b;

}
double add(double a, double b) {
	cout << "double add(double a, double b)"<<endl;
		return a + b;

}
*/
/*
int Add(int a, int b) {
	return a + b;
}

int Add(int a, int b, int c) { // 正确的重载：参数数量不同
	return a + b + c;
}

int main() {
	int sum1 = Add(10, 20);   
	double sum2 = Add(10,20,30); 

	cout << "sum1: " << sum1 << endl;
	cout << "sum2: " << sum2 << endl;
	return 0;
}

void f(int a, char b)
{
	cout << "f(int a,char b)" << endl;
}
void f(char b, int a)
{
	cout << "f(char b, int a)" << endl;
}
int main() {
	f(10, 'c');
	f('c', 10);
	return 0;
}
*/
// 下⾯两个函数构成重载
// f()但是调⽤时，会报错，存在歧义，编译器不知道调⽤谁
/*
void f()
{
	cout << "f()" << endl;
}
void f(int a = 10)
{
	cout << "f(int a)" << endl;
}
int main() {
	f();
	return 0;
}
*/
/*
int main() {
	int a = 520;
	int& b = a;
	int& c = a;
	int& d = b;
	++d;
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	cout << &d << endl;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	cout << d << endl;
	return 0;
}
*/
/*
#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	// 编译报错：“ra”: 必须初始化引⽤
	//int& ra;
	int& b = a;
	int c = 20;
	// 这⾥并⾮让b引⽤c，因为C++引⽤不能改变指向，
	// 这⾥是⼀个赋值
	b = c;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	b = 30;
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	return 0;
}
*/
/*
#include <iostream>
using namespace std;
void Swap(int& x, int& y) {
	int temp = x;
	x = y;
	y = temp;
}
int main() {
	int x = 520, y = 1314;
	cout << x << " " << y << endl;
	Swap(x, y);
	cout << x << " " << y << endl;

	return 0;
}
*/
/*
#include<iostream>
#include <assert.h>
using namespace std;
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void STInit(ST& rs, int n = 4)
{
	rs.a = (STDataType*)malloc(n * sizeof(STDataType));
	rs.top = 0;
	rs.capacity = n;
}
// 栈顶
void STPush(ST& rs, STDataType x)
{
	//assert(rs);
	// 满了， 扩容
	if (rs.top == rs.capacity)
	{
		printf("扩容\n");
		int newcapacity = rs.capacity == 0 ? 4 : rs.capacity * 2;
		STDataType* tmp = (STDataType*)realloc(rs.a, newcapacity *
			sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		rs.a = tmp;
		rs.capacity = newcapacity;
	}
	rs.a[rs.top] = x;
	rs.top++;
}
// int STTop(ST& rs)
int& STTop(ST& rs)
{
	assert(rs.top > 0);
	return rs.a[rs.top-1];
}
int main()
{
	// 调⽤全局的
	ST st1;
	STInit(st1);
	STPush(st1, 1);
	STPush(st1, 2);
	cout << STTop(st1) << endl;
		STTop(st1) += 10;
	cout << STTop(st1) << endl;
	return 0;
}
*/
/*
#include <iostream>
using namespace std;

void print(const int& x) {
	cout << x << endl;
	//x = 5; // 错误：不能通过 const 引用修改对象
}

int main() {
	int a = 10;
	const int& ref = a; // 创建一个对 a 的 const 引用
	//ref = 20; // 错误：不能通过 const 引用修改对象

	print(a); // 正确：传递 int
	print(20); // 正确：传递字面量，通过 const 引用允许

	return 0;
}
*/
#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	const int& ra = 30;
	// 编译报错: “初始化”: ⽆法从“int”转换为“int &”
	// int& rb = a * 3;
	const int& rb = a * 3;
	double d = 12.34;
	// 编译报错：“初始化”: ⽆法从“double”转换为“int &”
	// int& rd = d;
	const int& rd = d;
	return 0;
}